import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:raze/potions/raze_constant.dart';
import 'package:raze/potions/spatial_alchemy.dart';

/// 签名编辑弹窗组件
class SignatureEditDialog {
  /// 显示签名编辑弹窗
  /// [initialSignature] 当前签名
  /// [onSignatureUpdated] 签名更新成功的回调
  static void revealSignatureEdit({
    required String initialSignature,
    required Function(String newSignature) onSignatureUpdated,
  }) {
    Get.dialog(
      _SignatureEditWidget(
        initialSignature: initialSignature,
        onSignatureUpdated: onSignatureUpdated,
      ),
      barrierDismissible: true,
    );
  }
}

/// 签名编辑弹窗内部组件
class _SignatureEditWidget extends StatefulWidget {
  final String initialSignature;
  final Function(String newSignature) onSignatureUpdated;

  const _SignatureEditWidget({
    required this.initialSignature,
    required this.onSignatureUpdated,
  });

  @override
  State<_SignatureEditWidget> createState() => _SignatureEditWidgetState();
}

class _SignatureEditWidgetState extends State<_SignatureEditWidget> {
  late final TextEditingController _signatureController;
  final RxBool _isSignatureValid = false.obs;

  @override
  void initState() {
    super.initState();
    _signatureController = TextEditingController(text: widget.initialSignature);
    _signatureController.addListener(_validateSignature);
    _validateSignature();
  }

  @override
  void dispose() {
    _signatureController.dispose();
    super.dispose();
  }

  /// 验证签名
  void _validateSignature() {
    _isSignatureValid.value = _signatureController.text.trim().isNotEmpty;
  }

  /// 保存签名
  void _executeSave() {
    final newSignature = _signatureController.text.trim();
    if (newSignature.isEmpty) {
      return;
    }

    Get.back();
    widget.onSignatureUpdated(newSignature);
  }

  @override
  Widget build(BuildContext context) {
    return Dialog(
      backgroundColor: Colors.transparent,
      elevation: 0,
      child: Material(
        color: Colors.transparent,
        child: Container(
          margin: EdgeInsets.symmetric(horizontal: 24.w),
          padding: EdgeInsets.all(24.w),
          decoration: BoxDecoration(
            color: RazeConstants.white,
            borderRadius: BorderRadius.circular(24.r),
            border: Border.all(color: RazeConstants.black, width: 2.w),
          ),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              // 标题区域
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text('Edit Signature')
                      .applyTypeSize(22.sp)
                      .applyTypeColor(RazeConstants.black)
                      .applyBagelFatOne(weight: FontWeight.w400),
                  GestureDetector(
                    onTap: () => Get.back(),
                    child: Icon(
                      Icons.close_rounded,
                      size: 24.sp,
                      color: RazeConstants.black,
                    ),
                  ),
                ],
              ),
              20.h.boxHeight,

              // 签名输入框
              Container(
                constraints: BoxConstraints(minHeight: 80.h, maxHeight: 120.h),
                padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 12.h),
                decoration: BoxDecoration(
                  color: RazeConstants.background,
                  borderRadius: BorderRadius.circular(16.r),
                  border: Border.all(
                    color: RazeConstants.black.withOpacity(0.2),
                    width: 1.5.w,
                  ),
                ),
                child: TextField(
                  controller: _signatureController,
                  maxLines: null,
                  maxLength: 100,
                  decoration: InputDecoration(
                    hintText: 'Enter your signature...',
                    hintStyle: TextStyle(
                      fontSize: 14.sp,
                      color: RazeConstants.black.withOpacity(0.4),
                      fontFamily: 'NotoSans',
                    ),
                    border: InputBorder.none,
                    isDense: true,
                    counterStyle: TextStyle(
                      fontSize: 12.sp,
                      color: RazeConstants.black.withOpacity(0.4),
                    ),
                  ),
                  style: TextStyle(
                    fontSize: 14.sp,
                    color: RazeConstants.black,
                    fontFamily: 'NotoSans',
                  ),
                ),
              ),
              24.h.boxHeight,

              // 按钮区域
              Row(
                children: [
                  // 取消按钮
                  Expanded(
                    child: Container(
                          height: 50.h,
                          decoration: BoxDecoration(
                            color: RazeConstants.white,
                            borderRadius: BorderRadius.circular(16.r),
                            border: Border.all(
                              color: RazeConstants.black,
                              width: 2.w,
                            ),
                          ),
                          child: Center(
                            child: Text('Cancel')
                                .applyTypeSize(16.sp)
                                .applyTypeColor(RazeConstants.black)
                                .applyBagelFatOne(weight: FontWeight.w400),
                          ),
                        )
                        .withRightBottomShadow(thickness: 1.5)
                        .withTouchResonance(onTap: () => Get.back()),
                  ),
                  16.w.boxWidth,

                  // 保存按钮
                  Expanded(
                    child: Obx(() {
                      final isValid = _isSignatureValid.value;
                      return Container(
                            height: 50.h,
                            decoration: BoxDecoration(
                              color:
                                  isValid
                                      ? RazeConstants.green
                                      : RazeConstants.black.withOpacity(0.2),
                              borderRadius: BorderRadius.circular(16.r),
                              border: Border.all(
                                color: RazeConstants.black,
                                width: 2.w,
                              ),
                            ),
                            child: Center(
                              child: Text('Save')
                                  .applyTypeSize(16.sp)
                                  .applyTypeColor(RazeConstants.white)
                                  .applyBagelFatOne(weight: FontWeight.w400),
                            ),
                          )
                          .withRightBottomShadow(thickness: 1.5)
                          .withTouchResonance(
                            onTap: isValid ? _executeSave : null,
                          );
                    }),
                  ),
                ],
              ),
            ],
          ),
        ).withRightBottomShadow(thickness: 2),
      ),
    );
  }
}
